<html>
<head>
<title>BusinessData Services</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<link rel="stylesheet" type="text/css" href="services.css"/>
</head>
<body>

<h1>Companies Restful Services</h1>
<div class="hostInfo">
<form action="">
<div>
<label for="scheme">Transport</label><select name="scheme" id="scheme">
<option value="http://">HTTP</option>
<option value="https://">HTTPS</option></select>
</div>
<div>
<label for="host">Host</label><select name="host" id="host"></select>
</div>
<div>
<label for="method">Method</label>GET<input type="hidden" name="method" id="method" value="GET">
</div>

</form>
</div>
<div class="services">
<div class="serviceInfo">
<form id="entity_search" class="serviceForm">
<h2>Entity Search</h2>
<div>
<label for="url">Base Url:</label>
<input type="text" name="url" value="/data/app/ws/rest/companies/entity/search/" readonly="readonly"/>
</div>
<div>
<label for="version">Version:</label>

<select name="version">
<option value="v1.0">v1.0</option>
<option value="v1.1">v1.1</option>
</select>
</div>
<div>
<label for="size">Result Set Page Size:</label>
<input type="text" name="size" maxlength="4"/>
<div class="hint">Default is 50</div>
</div>
<div>
<label for="page">Page Number:</label>
<input type="text" name="page" maxlength="4"/>
<div class="hint">Default is 0 (0 indexed)</div>
</div>


<div>
<label for="suffix">Suffix:</label>
<input type="text" name="suffix" value=""/>
<div class="hint">To use parameterized form start the suffix with a '?'.  If searching for a term containing a &amp;, / or % these must be manually encoded as %26, %2F and %25.</div>
</div>
<div>
<label for="serviceUrl">Service URL:</label>
<span class="action"></span>
</div>
<button onclick="dosubmit(this.form);return false;">Submit</button>
</form>
</div>
<div class="serviceInfo">
<form id="role_search" class="serviceForm">
<h2>Role Search</h2>
<div>
<label for="url">Base Url:</label>
<input type="text" name="url" value="/data/app/ws/rest/companies/role/search/" readonly="readonly"/>
</div>
<div>
<label for="version">Version:</label>

<select name="version">
<option value="v1.0">v1.0</option>
<option value="v1.1">v1.1</option>
</select>
</div>

<div>
<label for="role">Role:</label>

<select name="role">
<option value="undefined">Default is All</option>
<option value="ALL">Director & Shareholder</option>
<option value="DIR">Director Role Only</option>
<option value="SHR">Shareholder Only</option>
<option value="DDIR">Disqualified/Banned Director</option>
</select>
</div>
<div>
<label for="size">Result Set Page Size:</label>
<input type="text" name="size" maxlength="4"/>
<div class="hint">Default is 50</div>
</div>
<div>
<label for="page">Page Number:</label>
<input type="text" name="page" maxlength="4"/>
<div class="hint">Default is 0 (0 indexed)</div>
</div>

<div>
<label for="suffix">Suffix:</label>
<input type="text" name="suffix" value=""/>
<div class="hint">To use parameterized form start the suffix with a '?'.  If searching for a term containing a &amp;, / or % these must be manually encoded as %26, %2F and %25.</div>
</div>
<div>
<label for="serviceUrl">Service URL:</label>
<span class="action"></span>
</div>
<button onclick="dosubmit(this.form);return false;">Submit</button>
</form>
</div>
<div class="serviceInfo">
<form id="entity_detail" class="serviceForm">
<h2>Entity Detail</h2>
<div>
<label for="url">Base Url:</label>
<input type="text" name="url" value="/data/app/ws/rest/companies/entity/" readonly="readonly"/>
</div>
<div>
<label for="version">Version:</label>

<select name="version">
<option value="v1.0">v1.0</option>
<option value="v1.1">v1.1</option>
</select>
</div>
<div>
<label for="suffix">Suffix:</label>
<input type="text" name="suffix" value=""/>
</div>
<div class="hint">You should use the companyNumber value found in the Enity Search result above</div>
<div>
<label for="serviceUrl">Service URL:</label>
<span class="action"></span>
</div>
<button onclick="dosubmit(this.form);return false;">Submit</button>
</form>
</div>
</div>
</div>
<form id="actionform" target="_blank"></form>
<script type="text/javascript">
var topWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIWebNavigation)
                   .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                   .rootTreeItem
                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                   .getInterface(Components.interfaces.nsIDOMWindow);
var BusinessDataChrome = topWindow.BusinessDataChrome; 

function dosubmit(form){
BusinessDataChrome.forceSaveDialog = false;
   var $host = $('#host');
   var $scheme = $('#scheme option:selected');
   var $method = $('#method');
   var $form = $('#actionform'); 
   $form.attr('method', $method.val());
   var $action = $('span.action', form);
   $form.attr('action', $scheme.val()+$host.val()+$action.text());
  // alert($scheme.val()+$host.val()+$action.text());
  // $form.get(0).submit();
   window.open($scheme.val()+$host.val()+$action.text());
}

$(function(){
  BusinessDataChrome.initAccessInfo();
  var sel = $('#host').get(0);
  for(key in BusinessDataChrome.accessInfo){
    var op = new Option(key, key,false);
    sel.options.add(op);
  }
  $('form.serviceForm').each(function(ind){
   makeUri(this);
    $(this).change(function(){
   /* var $host = $('#host option:selected');
    var $url = $('input[name=url]', this);
    var $version = $('select[name=version] option:selected', this);
    var $suffix = $('input[name=suffix]', this);
    var uri = $url.val()+$version.val()+'/'+escape($suffix.val());
    if ($host.val() == 'localhost'){
        uri = uri.replace(/\/data\/app\/ws/, ':8080/fmit-g2b-webservice');
    }
    $('span.action',this).html(uri);(*/
    makeUri(this);
    });
  });
});

function makeUri(frm){
 var $host = $('#host option:selected');
    var $url = $('input[name=url]', frm);
	
    var $version = $('select[name=version] option:selected', frm);
	var $role = $('select[name=role] option:selected', frm);
    var $suffix = $('input[name=suffix]', frm);
    var uri = $url.val()+$version.val();
	if ($url.val().indexOf("search") >= 0){
		var $page = $('input[name=page]', frm);
		var $size = $('input[name=size]', frm);
		if($page.val() != 0){
			uri += '/page/' + $page.val() ;
		}
		if($size.val() != 0){
			uri += '/size/' + $size.val() ;
		}
	}
	
	if(($role.val() != undefined) && ($role.val() != "undefined"))
	{
		uri += '/type/' + $role.val() ;
	}
    if ($suffix.val().indexOf('?') == 0){
        uri += encodeURI($suffix.val()); 
    } else {
        uri +='/'+encodeURI($suffix.val());
    }
    if ($host.val() == 'localhost'){
        uri = uri.replace(/\/data\/app\/ws/, ':8080/fmit-g2b-webservice');
    }
    $('span.action',frm).html(uri);
}
</script>
</body>
</html>
